/**
 * 
 */
package org.cnbi.utils.chart.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.cnbi.utils.chart.Constants;
import org.cnbi.utils.chart.base.BaseChart;
import org.cnbi.utils.chart.component.Column;
import org.cnbi.utils.chart.xml.component.XmlCategories;
import org.cnbi.utils.chart.xml.component.XmlData;
import org.cnbi.utils.chart.xml.component.XmlDataSet;
import org.cnbi.utils.chart.xml.component.XmlStyles;
import org.cnbi.utils.expression.ExpressionParserUtil;
import org.cnbi.utils.pojo.DynamicBean;
/**
 * @company 中国经邦
 * @author Administrator
 * @date: 2014年3月14日下午5:29:03
 * @version: V1.0 多系列数据图表
 */
@XmlRootElement(name = "chart")
public class XmlSeriesChart extends BaseChart{
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<XmlCategories> categories ;
	
	private List<XmlDataSet> dataset = new ArrayList<XmlDataSet>();
	
	
	private XmlStyles styles;

	public XmlSeriesChart() {
	}
	


	/**
	 * @param categories
	 * @param dateSet
	 * @param chart
	 * @param styles
	 */
	public XmlSeriesChart(String []seriesNames,List<XmlData> datas, BaseChart chart, XmlStyles styles) {
		setMainData(seriesNames, datas);
		this.styles = styles;
	}
	
	public XmlSeriesChart(String []seriesNames,List<XmlData> datas) {
		setMainData(seriesNames, datas);
		//this.chart = chart;
	}
	public XmlSeriesChart(List<Column> seriesNames, List<DynamicBean> datas) {
		setMainData(seriesNames, datas);
		//this.chart = chart;
	}
	private void setMainData(String[] seriesNames, List<XmlData> datas) {
		List<XmlData> categorie = new ArrayList<XmlData>();
		for(int i=0,len = seriesNames.length;i<len;i++){
			List<XmlData> datasets = new ArrayList<XmlData>();
			  for(int j=0,lj=datas.size();j<lj;j++){
				  XmlData values = datas.get(j);
				  XmlData value = new XmlData();
				  value.setValue(values.getValue());
				  datasets.add(value);
				  if(i ==0){
					  XmlData label = new XmlData();
					  label.setLabel(values.getLabel());
					  categorie.add(label);
				  }
			  }
			  dataset.add(new XmlDataSet(seriesNames[i],datasets));
		}
		categories  = new ArrayList<XmlCategories>();
		categories.add(new XmlCategories(categorie));
	}

	private void setMainData(List<Column> seriesNames, List<DynamicBean> datas) {
		List<XmlData> categorie = new ArrayList<XmlData>();
		String pro = Constants.INDEX+"0";
		for(int i=0,len = seriesNames.size();i<len;i++){
			List<XmlData> datasets = new ArrayList<XmlData>();
			Column col = seriesNames.get(i);
			String index = col.getIndex();
			  for(int j=0,lj=datas.size();j<lj;j++){
				  DynamicBean values = datas.get(j);
				  Object temp = values.getValue(index);
				  String tempValue = ExpressionParserUtil.ZEROVALUE;
				  if(null != temp){
					  tempValue = temp.toString();
				  }
				  if(index.equals(pro)){//必须要有index0属性
					  XmlData label = new XmlData();
					  label.setLabel(tempValue);
					  categorie.add(label);
				  }else{
					  XmlData value = new XmlData();
					  value.setValue(tempValue);
					  datasets.add(value);
				  }
			  }
			 if(!index.equals(pro)){
				 XmlDataSet set = new XmlDataSet(col.getSname(),datasets);
				 if(null!=col.getColor()){
					 set.setColor(col.getColor());
				 }
				 dataset.add(set);
			 }
		}
		categories  = new ArrayList<XmlCategories>();
		categories.add(new XmlCategories(categorie));
	}

	

	

	@XmlElement
	public List<XmlCategories> getCategories() {
		return categories;
	}

	public void setCategories(List<XmlCategories> categories) {
		this.categories = categories;
	}



	@XmlElement
	public List<XmlDataSet> getDataset() {
		return dataset;
	}

	public void setDataset(List<XmlDataSet> dataset) {
		this.dataset = dataset;
	}
	@XmlElement
	public XmlStyles getStyles() {
		return styles;
	}

	public void setStyles(XmlStyles styles) {
		this.styles = styles;
	}

}
